package com.yunjian.core.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunjian.common.utils.*;
import com.yunjian.core.entity.*;
import com.yunjian.core.service.IAccountService;
import com.yunjian.core.service.IPurchaseDetailService;
import com.yunjian.core.service.IPurchaseOrderService;
import com.yunjian.core.service.SysUserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/Order")
public class SysAppUserOrderController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private IPurchaseOrderService purchaseOrderService;

    @Autowired
    private IPurchaseDetailService purchaseDetailService;

    @Autowired
    private IAccountService accountService;

    /**
     * 分页查询app用户订单列表
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        logger.info("分页查询app用户订单列表{}", JsonUtil.toJsonString(params));

        SysUserEntity user = HttpContextUtils.getLoginSysUserEntity();
        logger.info("当前登录用户{}", JsonUtil.toJsonString(user));
        SysUserRoleEntity role = sysUserRoleService.getOne(new QueryWrapper<SysUserRoleEntity>()
                .eq("user_id", user.getUserId()));
        int isAdmin = 0;
        if(role!=null && (role.getRoleId().intValue() == Constant.SUPER_ADMIN || role.getRoleId().intValue() == Constant.SUPER_DEALER)) {
            isAdmin = 1;
        }
        params.put("isAdmin", isAdmin);
        PageUtils page = purchaseOrderService.queryPage(params);

        return R.ok().put("page", page).put("isAdmin", isAdmin);
    }

    /**
     * 查询app用户订单详情
     */
    @PostMapping("/info")
    public R info(@RequestBody Map<String, Object> params){
        logger.info("查询app用户订单详情{}", JsonUtil.toJsonString(params));

        String id = StringUtil.obj2String(params.get("id"));

        PurchaseOrder order = purchaseOrderService.getOne(new QueryWrapper<PurchaseOrder>()
                .eq("id", Integer.parseInt(id)));

        List<PurchaseDetail> details = purchaseDetailService.list(new QueryWrapper<PurchaseDetail>()
                .eq("order_no", order.getOrderNo()));

        return R.ok().put("order", order).put("details", details);
    }

    /**
     * 修改app用户采购订单状态
     */
    @PostMapping("/updateOrderStatus")
    public R updateOrderStatus(@RequestBody Map<String, Object> params){
        logger.info("修改app用户采购订单状态{}", JsonUtil.toJsonString(params));
        String orderId = StringUtil.obj2String(params.get("orderId"));
        String status = StringUtil.obj2String(params.get("status"));
        PurchaseOrder order = purchaseOrderService.getOne(new QueryWrapper<PurchaseOrder>()
                .eq("id", Integer.parseInt(orderId)));



        order.setStatus(Integer.parseInt(status));
        purchaseOrderService.saveOrUpdate(order);
        if (Integer.parseInt(status) == 3){     //推送信息给用户
            Account account = accountService.getOne(new QueryWrapper<Account>()
                    .eq("id", order.getAccountId()));

//            MessageCenter msg = new MessageCenter();
//            msg.setAccountEmail(account.getEmail());
//            msg.setAccountId(account.getId());
//            msg.setContent("您的编号为"+order.getOrderNo()+"的订单已经被确认");
//            msg.setTitle("订单确认通知");
//            msg.setCreateTime(new Date());
//            msg.setCreatorId(Long.parseLong(a.getDealerId()+""));
//            msg.setDeleteFlag(1);
//            msg.setIsRead(0);
//            msg.setUpdateTime(new Date());
//            msgList.add(msg);
        }
        return R.ok();
    }
}
